Add python widget for Bloch Sphere - #3595
Conversation
Bill Ticehurst (billti)
left a comment
There was a problem hiding this comment.
To minimize the impact of KaTeX on the Python package:
KaTeX CSS and fonts are isolated in a Bloch-specific stylesheet, so existing widgets continue loading the original lightweight stylesheet.
The production build retains only the four .woff2 font faces used by the Bloch sphere’s matrices and math expressions, reducing the Bloch stylesheet from approximately 383 KB to 109 KB raw (264 KB to 60 KB compressed).
The index.js file for the widget however is 600kb larger after this PR (which is already too large as we still need to move the molecule visualizer out of it). This loads in every cell that just wants to show a histogram or a circuit or whatever.
As discussed, we should figure out how to make this a separate entry point so it only gets loaded when the user wants to see a Bloch sphere (which will prbably be pretty rarely). I can give you some guidance if needed.
Domingo Morales Lizama (domorale)
left a comment
There was a problem hiding this comment.
I built and tested the PR locally with the repository's Rust 1.95 toolchain. I left two inline comments:
- The generated widget wheel embeds KaTeX assets but appears not to include KaTeX's MIT license notice.
- Updating the synchronized
initial_gatestrait after display does not update the mounted Bloch Sphere; I included a minimal notebook reproducer and screenshot.
The widget otherwise built successfully and worked as expected in JupyterLab.
I've separated the bloch sphere into its own entry point with bloch.tsx, and removed it from index.tsx. |
| _initial_gates = traitlets.Unicode("").tag(sync=True) | ||
|
|
||
| @property | ||
| def initial_gates(self): |
There was a problem hiding this comment.
Why is this property here?
There was a problem hiding this comment.
This makes the initial_gates read-only by letting me define a getter without a setter. This helps avoid the object having a state that can escape the GUI, reducing the complexity and avoiding situations where the GUI and the python object are out of sync.
Adds python support for the Bloch Sphere visualizer by making it a python widget, which is then added under the qsharp_widgets package, and additionally made available under the QDK python package.
Example python usage:
To minimize the impact of KaTeX on the Python package: